package pers.zc.activiti.service.impl;

import org.activiti.engine.impl.util.json.JSONObject;
import pers.zc.activiti.entity.Entity;

import java.util.ArrayList;
import java.util.List;

/**
 * @Annotion:
 * @ClassName: TreeMethod
 * @Author: Lin Fuyuan
 * @Date: 2019/10/8  18:01
 * @Version: 1.0
 */
public class TreeMethod {



    /**
     *
     */
    public static void main(String[] args){
        List<Entity> list = new ArrayList<>();
        list.add(new Entity(1L, 0L));
        list.add(new Entity(2L, 1L));
        list.add(new Entity(3L, 1L));
        list.add(new Entity(4L, 2L));
        list.add(new Entity(5L, 2L));
        list.add(new Entity(6L, 3L));
        list.add(new Entity(7L,3L));
        list.add(new Entity(8L, 4L));
        list.add(new Entity(9L, 4L));
        list.add(new Entity(10L, 6L));
        list.add(new Entity(11L, 6L));
        list.add(new Entity(12L, 6L));
        list.add(new Entity(13L, 7L));
        Entity entity = new Entity(1L,0L );
        Entity childs = TreeMethod.findChilds(list, entity);
        JSONObject obj = new JSONObject(childs);
        System.out.println(obj.toString());

    }

    public  static Entity findChilds(List<Entity> list,Entity entity){
        for (Entity e:list){
            if(e.getFid().equals(entity.getId())){
                if(entity.getChildList()==null){
                    entity.setChildList(new ArrayList<Entity>());
                }
                entity.getChildList().add(findChilds(list,e ));
            }
        }
        return entity;
    }





}
